<template>
  <el-dialog v-bind="dialog" :visible.sync="dialog.visible" @open="open">
    <el-form ref="setPasswordForm" :model="setPasswordForm" status-icon :rules="rules" label-width="100px" :hide-required-asterisk="true">
      <el-form-item v-if="userInfo.hasPassword" label="原密码：" prop="orgPassword">
        <el-input v-model.trim="setPasswordForm.orgPassword" size="medium" type="password" autocomplete="off" placeholder="请输入原密码" />
      </el-form-item>

      <el-form-item label="新密码：" prop="newPassword">
        <el-input v-model.trim="setPasswordForm.newPassword" size="medium" type="password" autocomplete="off" placeholder="请输入新密码" />
      </el-form-item>

      <el-form-item label="确认密码：" prop="repeatPassword">
        <el-input v-model.trim="setPasswordForm.repeatPassword" size="medium" type="password" autocomplete="off" placeholder="请再次输入新密码" />
      </el-form-item>

      <el-form-item align="right">
        <el-button type="primary" size="mini" @click="handleSave">提 交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { validatePassword } from '@/utils/validate.js'
import { mapGetters } from 'vuex'
import { ucApi } from '@/api/api.js'

export default {
  name: 'SetPassword',
  data() {
    const validateRepeatPassword = (rule, value, callback) => {
      if (value !== this.setPasswordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      dialog: {
        visible: false,
        title: '',
        closeOnPressEscape: false,
        closeOnClickModal: false
      },
      setPasswordForm: {
        orgPassword: '',
        newPassword: '',
        repeatPassword: ''
      },
      rules: {
        orgPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        repeatPassword: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          { validator: validateRepeatPassword, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    //弹窗打开时，清除所有校验
    open() {
      this.$nextTick(() => {
        this.$refs['setPasswordForm'].resetFields()
      })
    },
    handleSave() {
      this.$refs.setPasswordForm.validate(async (valid) => {
        if (valid) {
          const { orgPassword, newPassword } = this.setPasswordForm
          this.$request(ucApi.updatePassword, { orgPassword, newPassword }).then((res) => {
            if (res.code === 1) {
              this.$message({
                message: '密码修改成功，请重新登录',
                type: 'success'
              })
              setTimeout(this.logout(), 500)
            }
          })
        }
      })
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog__wrapper ::v-deep.el-dialog {
  max-width: 364px;
}
</style>
